
/**
 * @ProjectName: 
 * @Copyright: 2014 lisheng  All Right Reserved.
 * @address: toughheart@163.com
 * @date: 2014年12月31日 下午1:31:51
 * @Description: 本内容未经本人允许禁止使用、转发.
 */
 
package com.ls.fw.data.search.impl.es.support;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.Serializable;

import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.repository.support.MappingElasticsearchEntityInformation;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;


/**
 * 
 * @author lisheng
 * @date 2014年12月31日 下午1:31:51
 * @version V1.0 
 */
public class EntityInformationFactory {

  public static <M, PK extends Serializable> MappingElasticsearchEntityInformation<M, PK> create(Class<M> cla) throws NoSuchFieldException, SecurityException, IntrospectionException{
		
		TypeInformation<M> typeInformation = ClassTypeInformation.from(cla);
		
		MyElasticsearchPersistentProperty persistentProperty = new MyElasticsearchPersistentProperty(
				  cla.getDeclaredField("id")
				, new PropertyDescriptor("id", cla)
				, new SimpleElasticsearchPersistentEntity<M>(typeInformation)
				, new SimpleTypeHolder()
		);
		SimpleElasticsearchPersistentEntity<M> entity = new SimpleElasticsearchPersistentEntity<M>(typeInformation);
		entity.addPersistentProperty(persistentProperty);
		return new MappingElasticsearchEntityInformation<M, PK>(entity, cla.getSimpleName(), cla.getSimpleName());
	}
	 
}
